<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>泰坦尼克号生存预测</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <!-- 引入jQuery -->
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #ffffff; /* 将背景改为白色 */
            color: #000000; /* 字体颜色改为黑色 */
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        #container {
            background-color: #f8f9fa; /* 容器背景颜色为淡灰色 */
            border-radius: 10px;
            padding: 20px;
            width: 600px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
        h1 {
            text-align: center;
        }
        .row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
        }
        .row input {
            width: 100%;
            padding: 8px;
            margin-left: 5px;
            border: 1px solid #cccccc;
            border-radius: 5px;
            background-color: #ffffff;
            color: #000000;
        }
        .row label {
            width: 40%;
            text-align: right;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #575fcf;
            color: #ffffff;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
        }
        button:hover {
            background-color: #4b59c0;
        }
        #result {
            margin-top: 20px;
            padding: 10px;
            background-color: #f8f9fa;
            border-radius: 5px;
            border: 1px solid #cccccc;
            color: #000000;
        }
        #chatgpt-button {
            position: absolute;
            top: 10px;
            right: 10px;
            background-color: #007bff;
            color: #ffffff;
            border: none;
            border-radius: 5px;
            padding: 10px 15px;
            font-size: 14px;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            width: auto; /* 调整宽度 */
            min-width: 120px; /* 设置最小宽度 */
        }
        #chatgpt-button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <button id="chatgpt-button" onclick="window.location.href='http://127.0.0.1:8000/chatgpt/'">跳转到 ChatGPT</button>
    <div id="container">
        <h1>泰坦尼克号生存预测</h1>
        <form id="predictForm">
            <div class="row">
                <label for="pclass">船舱等级 (Pclass):</label>
                <input type="number" id="pclass" name="pclass" value="2" required>
            </div>
            <div class="row">
                <label for="sex">性别 (Sex: 0=男, 1=女):</label>
                <input type="number" id="sex" name="sex" value="0" required>
            </div>
            <div class="row">
                <label for="age">年龄 (Age):</label>
                <input type="number" id="age" name="age" value="34" required>
            </div>
            <div class="row">
                <label for="sibsp">兄弟姐妹或配偶数量 (SibSp):</label>
                <input type="number" id="sibsp" name="sibsp" value="0" required>
            </div>
            <div class="row">
                <label for="parch">父母或子女数量 (Parch):</label>
                <input type="number" id="parch" name="parch" value="0" required>
            </div>
            <div class="row">
                <label for="fare">票价 (Fare):</label>
                <input type="number" id="fare" name="fare" value="13" required>
            </div>
            <div class="row">
                <label for="embarked">登船港口 (Embarked: 0=C, 1=Q, 2=S):</label>
                <input type="number" id="embarked" name="embarked" value="2" required>
            </div>
            <button type="button" id="predictButton">预测生还概率</button>
        </form>

        <div id="result"></div> <!-- 用于显示预测结果 -->
    </div>

    <script>
        // 使用 jQuery 绑定按钮点击事件
        $(document).ready(function() {
            $('#predictButton').click(function(event) {
                // 阻止表单默认提交行为
                event.preventDefault();

                // 获取用户输入的数据
                const pclass = $('#pclass').val();
                const sex = $('#sex').val();
                const age = $('#age').val();
                const sibsp = $('#sibsp').val();
                const parch = $('#parch').val();
                const fare = $('#fare').val();
                const embarked = $('#embarked').val();

                // 校验输入
                if (!pclass || !sex || !age || !sibsp || !parch || !fare || !embarked) {
                    alert('请填写所有字段！');
                    return;
                }

                // 发起 AJAX 请求
                $.ajax({
                    url: '/predict', // AJAX 请求的 URL
                    type: 'GET', // 请求方法
                    data: {
                        pclass: pclass,
                        sex: sex,
                        age: age,
                        sibsp: sibsp,
                        parch: parch,
                        fare: fare,
                        embarked: embarked
                    }, // 请求参数
                    success: function(response) {
                        // 显示预测结果
                        if (response && response.survival_prob !== undefined) {
                            $('#result').html(`
                                <h2>预测结果：</h2>
                                <p>生存的概率是：${(response.survival_prob * 100).toFixed(2)}%</p>
                            `);
                        } else {
                            $('#result').html('<p>无法解析服务器返回的数据。</p>');
                            console.error('Unexpected response format:', response);
                        }
                    },
                    error: function(xhr, status, error) {
                        // 处理错误情况
                        $('#result').html('<p>发生错误，请重试。</p>');
                        console.error('Error:', error);
                    }
                });
            });
        });
    </script>
</body>
</html>
